package com.lottery.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lottery.entity.LotteryRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface LotteryRecordRepository extends BaseMapper<LotteryRecord> {
    
    /**
     * 根据用户ID查找抽奖记录，按时间倒序
     */
    @Select("SELECT * FROM lottery_records WHERE user_id = #{userId} ORDER BY created_at DESC")
    List<LotteryRecord> findByUserIdOrderByCreatedAtDesc(Long userId);
    
    /**
     * 查找用户的中奖记录
     */
    @Select("SELECT * FROM lottery_records WHERE user_id = #{userId} AND is_winner = true ORDER BY created_at DESC")
    List<LotteryRecord> findByUserIdAndIsWinnerTrueOrderByCreatedAtDesc(Long userId);
    
    /**
     * 统计用户总抽奖次数
     */
    @Select("SELECT COUNT(*) FROM lottery_records WHERE user_id = #{userId}")
    long countByUserId(Long userId);
    
    /**
     * 统计用户中奖次数
     */
    @Select("SELECT COUNT(*) FROM lottery_records WHERE user_id = #{userId} AND is_winner = true")
    long countByUserIdAndIsWinnerTrue(Long userId);
    
    /**
     * 查找指定时间范围内的抽奖记录
     */
    @Select("SELECT * FROM lottery_records WHERE created_at BETWEEN #{startTime} AND #{endTime} ORDER BY created_at DESC")
    List<LotteryRecord> findByCreatedAtBetween(@Param("startTime") LocalDateTime startTime, 
                                               @Param("endTime") LocalDateTime endTime);
    
    /**
     * 统计总中奖记录数
     */
    @Select("SELECT COUNT(*) FROM lottery_records WHERE is_winner = true")
    long countWinningRecords();
}